# coding=utf-8
import os
import json
from collections import defaultdict
# D:\tmp\tag\vnote.space\学习笔记\软件\操作系统\Windows
test_json = """
{
	"created_time": "2022-01-13T14:50:45Z",
	"files": [
		{
			"attachment_folder": "",
			"created_time": "2022-06-08T01:07:47Z",
			"id": "979",
			"modified_time": "2022-06-12T02:00:39Z",
			"name": "2022-06-08：在Windows搭建基于VS2019的OpenCV开发环境.md",
			"signature": "27205977503331",
			"tags": [
				"opencv",
				"Windows"
			]
		},
		{
			"attachment_folder": "190860510227451",
			"created_time": "2022-11-16T12:09:31Z",
			"id": "1157",
			"modified_time": "2022-11-16T12:11:13Z",
			"name": "2022-11-16：Windows安装.net 3.5.md",
			"signature": "177762259707",
			"tags": [
				"Windows",
				"python"
			]
		}
	],
	"folders": [
	],
	"id": "978",
	"modified_time": "2022-01-13T14:50:45Z",
	"signature": "177735744581",
	"version": 3
}
"""

tmp_tag_list_cache = list()
split_mark = '::'
all_tag_dict = defaultdict(set)


# 读取文件到json
def file_to_json(filename):
	try:
		with open (filename, encoding="UTF-8") as f:
			f.read()
			return f.decode('UTF-8')
	except Exception as e:
		return r'{}'

# 把json文件处理为行数据
def json_to_list_datas(json_str) :
	header_dic = json.loads(json_str)


	files = header_dic['files']
	#print(files)
	for file_info in files:
		filename = file_info['name']
		tags = file_info['tags']
		#print()
		for tag in tags:
			#print(tag + ' <--:' + filename)
			tmp_tag_list_cache.append(tag + split_mark + filename)
			#all_tag_dict[tag] = 

# 从行数据构造为字典，key为tag，value为set
# https://www.cnpython.com/qa/689087
def line_datas_to_dict() :
	for line in tmp_tag_list_cache:
		c1, c2 = line.split(split_mark)
		all_tag_dict[c1].add(c2)
		#print(all_tag_dict)
	#print(all_tag_dict)

# 查询字典里面的set（用于后续）
def dump_dict():
	# 遍历字典
	for key in all_tag_dict:
		#print(item.key())
		#print(item)
		#print(item[0] + "--" + item[1])
		for file in all_tag_dict[key]:
			print(key + ' <--:' + file)
			pass
			#print(' <--:' + file)
# 判断文件是否有某个标签
def is_file_in_tag(filename, tag):
	if filename in all_tag_dict[tag]:
		return True
	return False

def get_all_set() :
	result_set = set()
	for key in all_tag_dict:
		#print(item.key())
		#print(item)
		#print(item[0] + "--" + item[1])
		result_set = result_set | all_tag_dict[key]
	return result_set

# 查看文件所属的标签
def list_file_in_which_set(filename) :
	# 遍历字典
	result = set()
	for key in all_tag_dict:
		#print(item.key())
		#print(item)
		#print(item[0] + "--" + item[1])
		if is_file_in_tag(filename, key) :
			result.add(key)
	return result

# 获取标签对应的文件集合
def get_tag_set(tagname):
	return all_tag_dict[tagname]

# 在当前集合，探索出可以搜索的tag
# 场景： 点击了一个标签，然后得到一个新的列表，在这个列表下求出其他标签
# 思路：点击标签以后，得到标签下的所有文件，根据这些文件，反算出文件所在的标签
def print_file_tag_which_in_set(set_):
	all_in_set = set()
	result_tag_set = set()
	for file in set_:
		result_tag_set |= list_file_in_which_set(file)

	print(result_tag_set)
	return result_tag_set

json_to_list_datas(test_json)

line_datas_to_dict()

#dump_dict()

#is_file_in_tag("2022-06-08：在Windows搭建基于VS2019的OpenCV开发环境.md", "Windows")

#print(is_file_in_tag("2022-11-16：Windows安装.net 3.5.md", "opencv"))

#print(list_file_in_which_set('2022-11-16：Windows安装.net 3.5.md'))
#print(list_file_in_which_set('2022-06-08：在Windows搭建基于VS2019的OpenCV开发环境.md'))
all_set = get_all_set()
print_file_tag_which_in_set(all_set)

all_set = get_tag_set("opencv")
print_file_tag_which_in_set(all_set)
#print(all_tag_dict)